package com.ruixin.base.util;

import com.ruixin.base.exception.BaseException;

import java.util.Date;

/**
 * 类型转化工具类
 */
public class TypeConvertor {

    /**
     * 类型转化
     * @param value
     * @param type
     * @return
     * @throws BaseException
     */
    public static Object parseData(Object value,Class<?> type) throws BaseException {
        if(type == String.class){
            return toString(value);
        }else if(type == int.class||type == Integer.class){
            return toInteger(value);
        }else if(type == double.class || type == Double.class){
            return toInteger(value);
        }else if(type == float.class || type == Float.class){
            return toFloat(value);
        }else if(type == long.class || type == Long.class){
            return toLong(value);
        }else if(type == Date.class){
            return null;
        }else if(type == boolean.class || type == Boolean.class){
            return toBoolean(value);
        }else{
            return value;
        }
    }

    /**
     * 转化为String
     * @param value
     * @return
     */
    public static String toString(Object value){
        if(value==null){
            return null;
        }else{
            return value.toString();
        }
    }

    /**
     * 转化为Long
     * @param value
     * @return
     */
    public static Long toLong(Object value){
        return Long.parseLong(toString(value));
    }

    /**
     * 转化为Integer
     * @param value
     * @return
     */
    public static Integer toInteger(Object value){
        return Integer.parseInt(toString(value));
    }

    /**
     * 转化为Float
     * @param value
     * @return
     */
    public static Float toFloat(Object value){
        return Float.parseFloat(toString(value));
    }

    /**
     * 转化为Boolean
     * @param value
     * @return
     */
    public static Boolean toBoolean(Object value){
        return Boolean.parseBoolean(toString(value));
    }
}
